package net.linku.utils;

/**
 * @ClassName test
 * @Description: 插入身份证对应的id信息 —— 临时用
 * @Author SilenceTian
 * @Date 2024/1/3
 * @Version V1.0.2
 **/

import com.google.gson.Gson;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

public class InsertIdCardAreaData {


    private static void insertIdCardAreaData(Connection connection, String jsonData) throws SQLException {
        // 插入数据的 SQL 语句
        String insertSql = "INSERT INTO l_c_id_card_area (area_code, area_code_province, province, area_code_city, city, area_code_district, district," +
                " enabled_flag, object_version_number, creation_date, created_by, last_update_date, last_updated_by) VALUES" +
                " (?, ?, ?, ?, ?, ?, ?, '1', 1, CURRENT_TIMESTAMP, -1, CURRENT_TIMESTAMP, -1)";

        // 使用预处理语句
        try (PreparedStatement preparedStatement = connection.prepareStatement(insertSql)) {
            Gson gson = new Gson();
            // 解析 JSON 数据
            Map<String, Object> dataMap = gson.fromJson(jsonData, Map.class);
            System.out.println(dataMap.entrySet());
            // 插入数据
            insertData(preparedStatement, dataMap, null);
        }
    }

    private static void insertData(PreparedStatement preparedStatement, Map<String, Object> dataMap, String parentAreaCode) throws SQLException {
        for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
            String areaCode = entry.getKey();
            Object areaData = entry.getValue();

            if (areaData instanceof String) {
                // 处理部分区域只有一个字符串的情况
                // 这里的判断条件需要修改，仅在没有父区域或者有父区域但是不是省的情况下插入数据
                if (parentAreaCode == null || !parentAreaCode.equals(areaCode.substring(0, 2) + "0000")) {
                    preparedStatement.setString(1, areaCode);
                    preparedStatement.setString(2, areaCode.substring(0, 2));
                    preparedStatement.setString(3, (String) areaData);
                    preparedStatement.setString(4, areaCode.substring(2, 4));
                    preparedStatement.setString(5, (String) areaData);
                    preparedStatement.setString(6, areaCode.substring(4, 6));
                    preparedStatement.setString(7, (String) areaData);

                    // 执行插入操作
                    preparedStatement.executeUpdate();
                }
            } else if (areaData instanceof Map) {
                // 处理包含子区域的情况
                Map<String, Object> childData = (Map<String, Object>) areaData;

                // 设置参数
                preparedStatement.setString(1, areaCode);
                preparedStatement.setString(2, areaCode.substring(0, 2));
                preparedStatement.setString(3, childData.containsKey("name") ? (String) childData.get("name") : null);
                preparedStatement.setString(4, areaCode.substring(2, 4));
                preparedStatement.setString(5, childData.containsKey("name") ? (String) childData.get("name") : null);
                preparedStatement.setString(6, areaCode.substring(4, 6));
                preparedStatement.setString(7, childData.containsKey("name") ? (String) childData.get("name") : null);

                // 执行插入操作
                preparedStatement.executeUpdate();

                // 递归插入子区域
                if (childData.containsKey("child")) {
                    insertData(preparedStatement, (Map<String, Object>) childData.get("child"), areaCode);
                }
            }
        }
    }

    //    private static void insertData(PreparedStatement preparedStatement, Map<String, Object> dataMap, String parentAreaCode) throws SQLException {
//        for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
//            String areaCode = entry.getKey();
//            Object areaData = entry.getValue();
//
//            if (areaData instanceof String) {
//                // 处理部分区域只有一个字符串的情况
//                preparedStatement.setString(1, areaCode);
//                preparedStatement.setString(2, areaCode.substring(0, 2));
//                preparedStatement.setString(3, (String) areaData);
//                preparedStatement.setString(4, areaCode.substring(2, 4));
//                preparedStatement.setString(5, (String) areaData);
//                preparedStatement.setString(6, areaCode.substring(4, 6));
//                preparedStatement.setString(7, (String) areaData);
//
//                // 执行插入操作
//                preparedStatement.executeUpdate();
//            } else if (areaData instanceof Map) {
//                // 处理包含子区域的情况
//                Map<String, Object> childData = (Map<String, Object>) areaData;
//
//                // 设置参数
//                preparedStatement.setString(1, areaCode);
//                preparedStatement.setString(2, areaCode.substring(0, 2));
//                preparedStatement.setString(3, childData.containsKey("name") ? (String) childData.get("name") : null);
//                preparedStatement.setString(4, areaCode.substring(2, 4));
//                preparedStatement.setString(5, childData.containsKey("name") ? (String) childData.get("name") : null);
//                preparedStatement.setString(6, areaCode.substring(4, 6));
//                preparedStatement.setString(7, childData.containsKey("name") ? (String) childData.get("name") : null);
//
//                // 执行插入操作
//                preparedStatement.executeUpdate();
//
//                // 递归插入子区域
//                if (childData.containsKey("child")) {
//                    insertData(preparedStatement, (Map<String, Object>) childData.get("child"), areaCode);
//                }
//            }
//        }
//    }
    public static void main(String[] args) {
        // 数据库连接信息
        String jdbcUrl = "jdbc:mysql://localhost:3306/linku_localhost?useSSL=false&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "root";

        // 待插入的数据


        try {
            ClassPathResource classPathResource = new ClassPathResource("idCard.json");
            InputStream inputStream = classPathResource.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String jsonData = FileCopyUtils.copyToString(bufferedReader);
            System.out.println(jsonData);
            // 加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 建立数据库连接
            Connection connection = DriverManager.getConnection(jdbcUrl, username, password);

            // 解析 JSON 数据并插入到数据库
            insertIdCardAreaData(connection, jsonData);

            // 关闭数据库连接
            connection.close();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

